using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

using FreeFlow.Administration;
using FreeFlow.WinFormsControls.HelperClasses;

namespace FreeFlowAdministrator
{
	/// <summary>
	/// Summary description for UsersControl.
	/// </summary>
	public class UsersControl : AdminUserControl
	{
    private FreeFlowAdministrator.UIActionsControl uiActionsControl;
    private ExtendedDataGrid usersGrid;
    private IContainer components;

		public UsersControl()
		{
			// This call is required by the Windows.Forms Form Designer.
			InitializeComponent();

      UIActionList actionList = new UIActionList();
      actionList.Add("Refresh", "Refresh the list", new ActionExecuteDelegate(RefreshList));
      actionList.Add("Copy to clipboard", "Copy the list of users to the clipboard", 
        new ActionExecuteDelegate(CopyToClipboard));
      actionList.Add("Open selected users", "Open the selected users", 
        new ActionExecuteDelegate(OpenUsers));
      actionList.Add("Add user", "Add a new user to the system", 
        new ActionExecuteDelegate(AddUser));
      actionList.Add("Delete selected users", "Delete the selected users", 
        new ActionExecuteDelegate(DeleteUsers));
      uiActionsControl.Actions = actionList;
		}

		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Component Designer generated code
		/// <summary> 
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
      this.components = new System.ComponentModel.Container();
      this.uiActionsControl = new FreeFlowAdministrator.UIActionsControl();
      this.usersGrid = new FreeFlowAdministrator.ExtendedDataGrid();
      ((System.ComponentModel.ISupportInitialize)(this.usersGrid)).BeginInit();
      this.SuspendLayout();
      // 
      // uiActionsControl
      // 
      this.uiActionsControl.Actions = null;
      this.uiActionsControl.Dock = System.Windows.Forms.DockStyle.Right;
      this.uiActionsControl.Location = new System.Drawing.Point(578, 0);
      this.uiActionsControl.Name = "uiActionsControl";
      this.uiActionsControl.Size = new System.Drawing.Size(150, 416);
      this.uiActionsControl.TabIndex = 2;
      // 
      // usersGrid
      // 
      this.usersGrid.AllowNavigation = false;
      this.usersGrid.CaptionVisible = false;
      this.usersGrid.DataMember = "";
      this.usersGrid.Dock = System.Windows.Forms.DockStyle.Fill;
      this.usersGrid.HeaderForeColor = System.Drawing.SystemColors.ControlText;
      this.usersGrid.Location = new System.Drawing.Point(0, 0);
      this.usersGrid.Name = "usersGrid";
      this.usersGrid.Size = new System.Drawing.Size(578, 416);
      this.usersGrid.TabIndex = 3;
      this.usersGrid.OpenSelected += new System.EventHandler(this.usersGrid_OpenSelected);
      // 
      // UsersControl
      // 
      this.Controls.Add(this.usersGrid);
      this.Controls.Add(this.uiActionsControl);
      this.Name = "UsersControl";
      this.Size = new System.Drawing.Size(728, 416);
      ((System.ComponentModel.ISupportInitialize)(this.usersGrid)).EndInit();
      this.ResumeLayout(false);

    }
		#endregion

    public event EventHandler UserAdded;

    private string AddUser()
    {
      // add user
      string userName = InputBox.ShowInputBox("Add user");
      if (userName != null)
      {
        users.Add(userName);
        
        // refresh tree view
        if (UserAdded != null)
          UserAdded(this, EventArgs.Empty);

        usersGrid.RefreshGrid();
      }
      return null;
    }

    private UserCollection users;
    public void LoadUsers(UserCollection users)
    {
      this.users = users;
      usersGrid.DataSource = null;
      usersGrid.DataSource = users;
      usersGrid.ClearColumns();
      usersGrid.AddColumn("Name");
      usersGrid.AddColumn("EmailAddress", "Email address");
      usersGrid.AddColumn("ReportsTo", "Reports to");
      usersGrid.AddColumn("DistinguishedName", "Distinguished name");
      usersGrid.AutosizeColumns();
    }

    private string DeleteUsers()
    {
      if (MessageBox.Show("Delete the selected users?", "Confirm", MessageBoxButtons.YesNo) == DialogResult.Yes)
      {
        using (new CursorHandler())
        {
          int count = usersGrid.DeleteSelected();
          OnRefreshRequired();
          return count.ToString() + " users deleted";
        }
      }
      return null;
    }

    private string OpenUsers()
    {
      using (new CursorHandler())
      {
        CurrencyManager bm = (CurrencyManager)usersGrid.BindingContext[usersGrid.DataSource];
        for (int i = 0; i < bm.Count; i++) 
        { 
          if(usersGrid.IsSelected(i))
          {
            User user = (User)bm.List[i];
            UserForm form = new UserForm();
            form.LoadUser(user);
            form.Show();
          }
        }
      }
      return null;
    }

    private string RefreshList()
    {
      users.Refresh();
      LoadUsers(users);
      OnRefreshRequired();
      return null;
    }

    private string CopyToClipboard()
    {
      usersGrid.CopyToClipboard();
      return null;
    }

    private void usersGrid_OpenSelected(object sender, EventArgs e)
    {
      OpenUsers();
    }
	}
}
